package com.MybatisUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;


public class StringUtil {
    /**
     *
     *  必填校验
     * @param name 请求参数的名字
     * @param showName 请求参数的中文名字
     * @param req  请求对象
     * *@return 如果name参数存在，并且不会空则返回参数值，否则返回null
     * */
    public static String requiredString(String name,String showName, HttpServletRequest req) {
        String value = req.getParameter(name);
        if(value == null || value.trim().length()==0){
            req.setAttribute(name+"_error","必须填写"+showName+"！");
            return null;
        }
        return value.trim();
    }

    /**
     * * 对比两个请求参数
     *  @param field1  请求参数1
     * @param field2  请求参数2
     * @param req     请求对象
     * @return        filed1==field2，则返回true；否则返回false
     * */
    public static boolean fieldsCompare(String field1, String field2,HttpServletRequest req) {
        String value1 = req.getParameter(field1);
        String value2 = req.getParameter(field2);
        if(!value1.equals(value2)){
            req.setAttribute(field1+"_error","两次录入的信息不一致！");
            req.setAttribute(field2+"_error","两次录入的信息不一致！");
            return false;
        }
        return true;
    }
    public static int getInt(HttpServletRequest req, String id) {
        String value = req.getParameter(id);
        try {
            int intValue = Integer.parseInt(value);
            return intValue;
        }catch (Exception e){

        }
        return -1;
    }


    public static String getString(HttpServletRequest req, String name) {

        String value = req.getParameter(name);
        if (value !=null){
            value = value.trim();
            if (value.length() == 0){
                value = null;
            }
        }
        return value;
    }

    /**
     * 批量获取
     * @param req 请求对象
     * @param names 请求参数的名称数组
     * @param params  容纳请求参数的集合
     * @return
     */
    public static void getParameters(HttpServletRequest req,
                                       String[] names,
                                       Map<String, Object> params) {
        if(names !=null && names.length>0){
            for (int i=0 ;i<names.length ;i++){
                String name = names[i];
                String value = req.getParameter(name);
                if (value !=null){
                    value =value.trim();
                    if (value.length()==0){
                        value =null;
                    }

                }
                params.put(name,value);
            }
        }
    }


    public static void getInterval(HttpServletRequest req,
    String interval, String startTime, String endTime, Map<String, Object> params) {

        int intInterval = getInt(req,interval);//获取时间段数值
        Calendar cl = Calendar.getInstance();
        String endTimeValue = DateUtils.format(cl.getTime());

        if (intInterval<=0){//最近
            cl.add(Calendar.WEEK_OF_YEAR,-1);

        }else {
            cl.add(Calendar.MONTH,-intInterval);//前退一个月
        }

        String startTimeValue = DateUtils.format(cl.getTime());

        params.put(interval,intInterval);
        params.put(startTime,startTimeValue);
        params.put(endTime,endTimeValue);





    }

    public static void toSuccess(String info,
                                 String successPage,
                                 String returnPage,
                                 int count,
                                 HttpServletRequest req,
                                 HttpServletResponse resp) throws ServletException, IOException {


        req.setAttribute("info",info);
        req.setAttribute("returnPage",returnPage);
        req.setAttribute("count",count);
        req.getRequestDispatcher(successPage).forward(req,resp);
    }
}

